﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace timetable
{
    /// <summary>
    /// 该类用来对于分班之后的班级课程进行重新编号
    /// </summary>
    class ClassifiedCourse
    {
        public int people;
        public List<string> courseIndexes;

        public ClassifiedCourse()
        {
            courseIndexes = new List<string>();
            people = 0;
        }

        // 重写Add函数，自动添加人数
        public void Add(string courseIndex,int people)
        {
                courseIndexes.Add(courseIndex);
                this.people += people;
            
        }
    }

    /// <summary>
    /// 每一门课程的具体的课程列表：继承List类
    /// </summary>
    class CourseList : List<ClassifiedCourse>
    {
        public int Sum { set; get; }
        public bool IsMixed { set; get; }
    }

    /// <summary>
    /// 重新编号之后的课程列表
    /// </summary>
    class ClassifiedCourses
    {

        private static int MaxMixedPeople = 60;
        public Dictionary<string, CourseList> ClassAMixed { set; get; }
        public Dictionary<string, CourseList> ClassAAlone { set; get; }
        public Dictionary<string, CourseList> ClassBMixed { set; get; }
        public Dictionary<string, CourseList> ClassBAlone { set; get; }
        
        // 存储重新编号之后的班级索引数据
        public Dictionary<string, HashSet<int>> NewCourseIndex = new Dictionary<string, HashSet<int>>();

        public ClassifiedCourses()
        {
            // 初始化四种类型的数据
            ClassAAlone = new Dictionary<string, CourseList>
            {
                {"语文",new CourseList()},
                {"数学",new CourseList()},
                {"英语",new CourseList()},
                {"物理",new CourseList()},
                {"化学",new CourseList()},
                {"生物",new CourseList()},
                {"政治",new CourseList()},
                {"历史",new CourseList()},
                {"地理",new CourseList()},
                {"通技",new CourseList()},
                {"体育",new CourseList()},
                {"音乐",new CourseList()},
                {"美术",new CourseList()}
            };
            ClassAMixed = new Dictionary<string, CourseList>
            {
                {"语文",new CourseList()},
                {"数学",new CourseList()},
                {"英语",new CourseList()},
                {"物理",new CourseList()},
                {"化学",new CourseList()},
                {"生物",new CourseList()},
                {"政治",new CourseList()},
                {"历史",new CourseList()},
                {"地理",new CourseList()},
                {"通技",new CourseList()},
                {"体育",new CourseList()},
                {"音乐",new CourseList()},
                {"美术",new CourseList()}
            };
            ClassBAlone = new Dictionary<string, CourseList>
            {
                {"语文",new CourseList()},
                {"数学",new CourseList()},
                {"英语",new CourseList()},
                {"物理",new CourseList()},
                {"化学",new CourseList()},
                {"生物",new CourseList()},
                {"政治",new CourseList()},
                {"历史",new CourseList()},
                {"地理",new CourseList()},
                {"通技",new CourseList()},
                {"体育",new CourseList()},
                {"音乐",new CourseList()},
                {"美术",new CourseList()}
            };
            ClassBMixed = new Dictionary<string, CourseList>
            {
                {"语文",new CourseList()},
                {"数学",new CourseList()},
                {"英语",new CourseList()},
                {"物理",new CourseList()},
                {"化学",new CourseList()},
                {"生物",new CourseList()},
                {"政治",new CourseList()},
                {"历史",new CourseList()},
                {"地理",new CourseList()},
                {"通技",new CourseList()},
                {"体育",new CourseList()},
                {"音乐",new CourseList()},
                {"美术",new CourseList()}
            };
        }

        public string[] Add(string courseFormattedName, int people,int groupId= 0)
        {
            HashSet<int> set = new HashSet<int>();
            // 对原课程编号进行信息提取
            string courseName = courseFormattedName.Substring(0, 2);
            string classNum;
            if (Char.IsDigit(courseFormattedName.Substring(3, 1), 0))
            {
                classNum = courseFormattedName.Substring(2, 2);
            }
            else
            {
                classNum = courseFormattedName.Substring(2, 1);
            }
            

            string level = null;
            if (!Char.IsDigit(courseFormattedName.Substring(3, 1), 0))
            {
                level= courseFormattedName.Substring(3, 1);
            }
            else if (!Char.IsDigit(courseFormattedName.Substring(4, 1), 0))
            {
                level = courseFormattedName.Substring(4, 1);
            }

            string isSpecail = null;
            if (Char.IsDigit(courseFormattedName.Substring(4, 1), 0))
            {
                isSpecail = courseFormattedName.Substring(4, 1);
            }
            else if (Char.IsDigit(courseFormattedName.Substring(5, 1), 0))
            {
                isSpecail = courseFormattedName.Substring(5, 1);
            }

            CourseList courseList = new CourseList();
            ClassifiedCourse toAddClassifiedCourse; // 将要添加的原课程信息
            string[] courseNum = new string[3];
            courseNum[0] = level;
            courseNum[1] = isSpecail;

            // 开始分类
            switch (level+isSpecail)
            {
                case "选0":
                case "学0":
                case "A0":
                case "B0":
                    toAddClassifiedCourse = new ClassifiedCourse();
                    if (level == "A" || level == "学")
                    {
                        ClassAAlone.TryGetValue(courseName, out courseList);
                        
                    }
                    else
                        ClassBAlone.TryGetValue(courseName, out courseList);

                    toAddClassifiedCourse.Add(courseFormattedName, people);
                    courseList.Add(toAddClassifiedCourse);
                    
                    
                    break;
                case "选1":
                case "学1":
                case "A1":
                case "B1":
                    if (level == "A" || level == "学")
                    {
                        ClassAMixed.TryGetValue(courseName, out courseList);
                    }
                    else
                    {
                        ClassBMixed.TryGetValue(courseName, out courseList);
                    }
                    
                    courseList.IsMixed = true;
                    // 寻找混合班的最后一个，判断还能否加进去人
                    // 可以就加进去，不能就新建一个

                    if (courseList.Count != 0 && courseList.Last().people+people < MaxMixedPeople)
                    {
                        courseList.Last().Add(courseFormattedName, people);
                    }
                    else
                    {
                        toAddClassifiedCourse = new ClassifiedCourse();
                        toAddClassifiedCourse.Add(courseFormattedName, people);
                        courseList.Add(toAddClassifiedCourse);
                    }
                    break;
            }
            // 该课程的教学班信息
            courseNum[2] = courseList.Count.ToString();
            // 将该课程加入课程索引
            string newName = courseName + courseNum[0] + courseNum[1] + courseNum[2];

            //if (NewCourseIndex.ContainsKey(newName))
            //{
            //    NewCourseIndex.Remove(newName);     
            //}

            //foreach (string index in courseList.Last().courseIndexes)
            //{
            //    string classNum;
            //    if (Char.IsDigit(index.Substring(3, 1), 0))
            //    {
            //        classNum = index.Substring(2, 2);
            //    }
            //    else
            //    {
            //        classNum = index.Substring(2, 1);
            //    }
            //    set.Add(int.Parse(classNum));
            //}

            //NewCourseIndex.Add(newName, set);
            

            // 若是特别课程，则直接加入
            if (groupId != 0)
            {
                HashSet<int> list;
                NewCourseIndex.TryGetValue(newName,out list);
                if (list != null)
                {
                    list.Add(int.Parse(classNum)*100 + groupId);
                }
                else
                {
                    // 添加一个进去
                    NewCourseIndex.Add(newName,new HashSet<int>{int.Parse(classNum) * 100 + groupId});
                }
            }
            else // 如果是共同课程，仅仅就按照之前的不变
            {
                if (NewCourseIndex.ContainsKey(newName))
                {
                    NewCourseIndex.Remove(newName);
                }

                foreach (string index in courseList.Last().courseIndexes)
                {
                    
                    if (Char.IsDigit(index.Substring(3, 1), 0))
                    {
                        classNum = index.Substring(2, 2);
                    }
                    else
                    {
                        classNum = index.Substring(2, 1);
                    }
                    set.Add(int.Parse(classNum) * 100);
                }

                NewCourseIndex.Add(newName, set);

            }
            return courseNum;
        }

    }
}
